﻿#include "Car.h"

Car::Car(int x, int w, QObject* parent)
{
	_x = x;
	_y = 40;
	_width = w;

	anim = new QPropertyAnimation(this, "pos");
	anim->setTargetObject(this);
}

Car::~Car()
{
}

QRectF Car::boundingRect() const
{
	return QRectF(0, 0, 541, 94);
}

int Car::_height()
{
	return 15 + 15;
}

void Car::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
	Q_UNUSED(option);
	Q_UNUSED(widget);

	QPen pen;

	//画小车
	QBrush brush(QColor(0, 255, 255), Qt::SolidPattern);
	painter->setBrush(brush);
	painter->drawRect(0, 0, 59, 15);

	//显示位置文本
	pen.setColor(QColor(Qt::blue));
	painter->setPen(pen);
	painter->drawText(11, 12, QString::number(pos_num));

	pen.setColor(QColor(Qt::black));
	painter->setPen(pen);
	painter->drawArc(6, 16, 15, 15, 0, 16 * 360);
	painter->drawArc(9, 19, 9, 9, 0, 16 * 360);
	painter->drawArc(39, 16, 15, 15, 0, 16 * 360);
	painter->drawArc(42, 19, 9, 9, 0, 16 * 360);

}

int Car::scaled(int pos)
{
	if (pos <= 0) return _pos_last;

	return (int)(_width / 20000.0 * pos);

}

//移动
void Car::move(int target)
{
	int end_n = scaled(target);

	anim->setStartValue(QPoint(_pos_last, _y));
	anim->setEndValue(QPoint(end_n, _y));

	anim->setDuration(std::abs(end_n - _pos_last) / 2);
	anim->start();

	_pos_last = end_n;
}

void Car::set_y(int y)
{
	_y = y;
}

//设置方向
void Car::set_direct(bool to_east)
{
	is_to_east = to_east;
}
